package com.atguigu.tingshu.payment.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.payment.service.WxPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;


    /**
     * 选择微信支付：1.保存本地交易记录  2.集成微信SDK获取小程序拉起微信支付所需参数
     *
     * @param paymentType 支付类型：1301=订单  1302=充值
     * @param orderNo     订单编号
     * @return {timeStamp:"时间戳",nonceStr:"随机字符串",package:"统一下单接口返回的 prepay_id", paySign:"签名", signType:"签名算法"}
     */
    @GuiGuLogin
    @Operation(summary = "选择微信支付：1.保存本地交易记录  2.集成微信SDK获取小程序拉起微信支付所需参数")
    @PostMapping("/wxPay/createJsapi/{paymentType}/{orderNo}")
    public Result<Map<String, String>> getMiniProgramePayParams(@PathVariable String paymentType, @PathVariable String orderNo) {
        Map<String, String> map = wxPayService.getMiniProgramePayParams(paymentType, orderNo);
        return Result.ok(map);
    }


    /**
     * 下单或充值微信支付成功后，收到来自微信支付结果通知
     *
     * @return {code:"SUCCESS",message:"成功"}
     */
    @Operation(summary = "下单或充值微信支付成功后，收到来自微信支付结果通知")
    @PostMapping("/wxPay/notify")
    public Result<Map<String, String>> wxPaySuccessNotify(HttpServletRequest request) {
        Map<String, String> map = wxPayService.wxPaySuccessNotify(request);
        return Result.ok(map);
    }


    /**
     * 根据商户端订单编号查询支付结果
     * @param orderNo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "根据商户端订单编号查询支付结果")
    @GetMapping("/wxPay/queryPayStatus/{orderNo}")
    public Result<Boolean> queryPayStatus(@PathVariable String orderNo) {
        Boolean flag = wxPayService.queryPayStatus(orderNo);
        return Result.ok(flag);
    }


}
